package cn.attaching.security;

import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.Charset;
import java.security.InvalidKeyException;
import java.security.Key;

/**
 * DES的加密解密
 * 高杰创建于 2017/01/19.
 */
public class DesSecurity {

    public static String encrypt(String key,String source) throws Exception {
        if(source == null || source.length() ==0||key == null || key.length()<8 )
            return source;
        try {
            DESKeySpec deskey = new DESKeySpec(key.getBytes(Charset.forName("UTF-8")));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(deskey);

        } catch (InvalidKeyException e) {
            throw new Exception("加密失败",e);
        }
        return null;
    }


}
